/**
 * @author of2198
 * @date 2017/4/27.
 * @description
 */

import { Store } from "iflux2";
import { fromJS } from "immutable";
import PageManageActor from "./actor/page-manage-actor";
import { domainList } from "webapi";
import initOpenApi from "../common/initOpenApi";

declare let xDllContext: any;

export default class AppStore extends Store {
  bindActor() {
    return [new PageManageActor()];
  }

  //获取平台类型的btn列表
  getPlatformBtnList = async () => {
    let openApi = await initOpenApi();
    return openApi.SysDicService.getPlatformTabInfo().then(
      result => {
        this.dispatch("SET_PLATFORM_BTN_LIST", result);
      },
      err => {
        return Promise.reject(err);
      }
    );
  };

  //获取页面类型的btn列表
  getPageTypeBtnList = async ({ platform }) => {
    let openApi = await initOpenApi();
    return openApi.SysDicService.getPageTypeTabInfo(platform).then(
      result => {
        this.dispatch("SET_PAGE_TYPE_BTN_LIST", result);
      },
      err => {
        return Promise.reject(err);
      }
    );
  };

  //获取页面列表
  getPageList = async ({ platform, pageNo, pageSize }) => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .getPageInfoList({
        platform,
        pageNo,
        pageSize,
        excludePageTypeList: ["roulette", "recruit", "spread"]
      })
      .then(result => {
        if (result && result.pageInfoList) {
          this.dispatch("SET_PAGE_NO", parseInt(pageNo) + 1);
          this.dispatch("SET_PAGE_LIST", result);
        }
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //获取页面类型信息
  getPageTypeInfo = async () => {
    let openApi = await initOpenApi();
    return openApi.SysDicService.getPageTypeInfo().then(
      result => {
        if (result) {
          this.dispatch("SET_PAGE_TYP_INFO", result);
        }
      },
      err => {
        return Promise.reject(err);
      }
    );
  };

  //删除页面
  deletePage = async ({ platform, pageType, pageCode, pageSize, pageNo }) => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .deletePageInfo({
        platform,
        pageCode,
        pageType
      })
      .then(() => {
        this.getPageList({ platform, pageSize, pageNo });
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //设为首页
  activateIndexPage = async ({ platform, pageCode, pageSize }) => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .setActivated({
        pageCode,
        platform
      })
      .then(() => {
        this.getPageList({ platform, pageSize, pageNo: 0 });
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //获取模板列表
  getTemplateList = async ({ platform, includePageTypeList }) => {
    let openApi = await initOpenApi();
    return openApi.TplService
      .getTemplateList({
        platform,
        pageSize: -1,
        includePageTypeList,
        isShow: true
      })
      .then(result => {
        this.dispatch("SET_TEMPLATE_LIST", result);
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //获取域名信息
  getDomainInfo = async () => {
    return domainList()
      .then(res => {
        this.dispatch("SET_DOMAIN_LIST", fromJS(res));
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //设置页面名称
  setPageTitle = async ({ platform, pageCode, pageType, title, index }) => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .updatePageTitle({
        platform,
        pageType,
        pageCode,
        title
      })
      .then(() => {
        this.dispatch("UPDATE_PAGE_TITLE", { index, title });
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //获取用户激活首页信息
  getActivatedPageInfo = async platform => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .getActivatedPageInfo(platform)
      .then(result => {
        this.dispatch("SET_ACTIVATED_PAGE_INFO", result);
      })
      .catch(err => {
        return Promise.reject(err);
      });
  };

  //复制页面信息
  copyPageInfo = async id => {
    let openApi = await initOpenApi();
    return openApi.PageService.copyPageInfo(id).then(() => {}).catch(err => {
      return Promise.reject(err);
    });
  };
}
